const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    region: ['广东省', '广州市', '海珠区'],
    addr: "",
    industry: 1,
    explain: "",
    staff: "",
    // list: "",
    // date: "",
    userInfo: '', // 用户基本信息
    company_id: '', // 用户的企业ID
    companyInfo: {}, // 企业详情
    identity: '', // 用户身份
    identity_status: '', // 用户身份审核
    username: "",
    fontSetSize: 'small', // 用户设置的字体大小（'small'：小，'middle'：中，'large'：大）
  },

  /**
   * 获取用户个人资料
   */
  getPersonalInfo () {
    let token = wx.getStorageSync('token')
    app.http({
      config: {
        url: '/api/User/personal_info',
        data: {
          token
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'personalInfoRes')
      let userInfo = res.data.data
      this.setData({
        userInfo,
        identity: userInfo.identity,
        identity_status: userInfo.identity_status,
        company_id: userInfo.company_id
      })
      this.getCompanyInfo()
    }).catch(err => {
      console.log(err, 'personalInfoErr')
    })
  },

  /**
   * 获取用户企业详情
   */
  getCompanyInfo () {
    let token = wx.getStorageSync('token')
    app.http({
      config: {
        url: '/api/User/company_info',
        data: {
          token,
          company_id: this.data.company_id
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'companyInfoRes')
      let companyInfo = res.data.data
      if (companyInfo) {
        let region = []
        region.push(companyInfo.province)
        region.push(companyInfo.city)
        region.push(companyInfo.district)
        this.setData({
          companyInfo,
          name: companyInfo.company_name,
          region,
          addr: companyInfo.company_address,
          industry: companyInfo.industry_type,
          explain: companyInfo.Industry_description,
          staff: companyInfo.employees_number,
          username: companyInfo.apply_sketch
        })
      }
    }).catch(err => {
      console.log(err, 'companyInfoErr')
    })
  },

  //  获取企业名称填写值
  getNmae (e) {
    this.setData({
      name: e.detail.value
    })
  },

  //  获取选择地址填写值
  RegionChange: function (e) {
    console.log(e)
    this.setData({
      region: e.detail.value
    })
  },

  //  获取详细地址填写值
  getAddr (e) {
    this.setData({
      addr: e.detail.value
    })
  },

  //  获取所属行业填写值
  radiochange: function (e) {
    this.setData({
      industry: e.detail.value
    })
  },

  //  获取行业说明填写值
  getExplain (e) {
    this.setData({
      explain: e.detail.value
    })
  },

  
  //  获取员工人数填写值
  getStaff (e) {
    this.setData({
      staff: e.detail.value
    })
  },

  /*//  获取是否上市填写值
  getList (e) {
    this.setData({
      list: e.detail.value
    })
  },

  //  获取上市时间填写值
  getDate (e) {
    this.setData({
      date: e.detail.value
    })
  },
  */

  //  获取垫生名称填写值
  getUsername (e) {
    this.setData({
      username: e.detail.value
    })
  },

  // 发送填写信息验证
  onSend () {
    console.log(this.data.identity, this.data.identity_status)
    if (this.data.identity == 3) {
      wx.showModal({
        title: '提示',
        content: '您是企业员工，无法注册企业',
        showCancel: false
      })
    } else if (this.data.identity == 2 && this.data.identity_status != 4) {
      if (this.data.identity_status == 1 || this.data.identity_status == 2) {
        wx.showModal({
          title: '提示',
          content: '您已申请注册企业，请耐心等待审核',
          showCancel: false
        })
      } else if (this.data.identity_status == 3) {
        wx.showModal({
          title: '提示',
          content: '您已注册企业，无法再次注册',
          showCancel: false
        })
      }
    } else {
      let token = wx.getStorageSync('token')
      let company_name = this.data.name
      let employees_number = this.data.staff
      let province = this.data.region[0]
      let city = this.data.region[1]
      let district = this.data.region[2]
      let company_address = this.data.addr
      let industry_type = this.data.industry
      let Industry_description = this.data.explain
      let apply_sketch = this.data.username
      app.http({
        config: {
          url: '/api/User/create_company',
          data: {
            token,
            company_name,
            employees_number,
            province,
            city,
            district,
            company_address,
            industry_type,
            Industry_description,
            apply_sketch
          },
          method: 'POST'
        },
        isAuth: true
      }).then(res => {
        console.log(res, 'companyRegisterRes')
        if (res.data.code === 200) {
          wx.showModal({
            title: "注册成功",
            showCancel: false,
            success (res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1,
                })
              }
            }
          })
        }
      }).catch(err => {
        console.log(err, 'companyRegisterErr')
      })
    }
  },

  onLoad: function (options) {
    this.getPersonalInfo()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let fontSetSize = wx.getStorageSync('fontSetSize') // 本地缓存中用户设置的字体大小
    if (fontSetSize) {
      this.setData({
        fontSetSize
      })
    }
  },
})